from util.Route import *
from service import RoleManageService
# 查询系统拥有的角色 (分页 查询角色的所有信息)
@route("/role/get/all",methods = ['POST'])
def roleGetAll():
    data = request.json
    num = data.get("num")
    size = data.get("size")
    cdn = data.get('cdn')
    page = Page(num=num, size=size)  # Create a Page object
    r = RoleManageService.getRoleAll(page,cdn)
    return R.success(r)


# 查询系统拥有的角色 (分页 查询id 角色名)
@route("/role/get/name",methods = ['POST'])
def roleGetName():
    data = request.json
    num = data.get("num")
    size = data.get("size")
    page = Page(num=num, size=size)  # Create a Page object
    r = RoleManageService.getRoleName(page)
    return R.success(r)



# 修改角色信息（角色关键字，角色名称，启用禁用状态，角色的系统属性）
@route("/role/update", methods = ['POST'])
def roleUpdate():
    k:dict = request.json
    r = RoleManageService.updateRole(k)
    if r:
        return R.success(r,"角色信息修改成功")
    else:
        return R.create(400,None,'角色信息修改失败')

# 给系统添加角色
@route("/role/add", methods = ['POST'])
def roleAdd():
    uid = request.environ.get("UID")
    data = request.json
    r = RoleManageService.addRole(uid,data)
    if r:
        return R.success(r,"角色创建成功")
    else:
        return R.bad_request('角色创建失败')
# 删除系统角色   
@route("/role/delete",methods = ['POST'])
def roleDelete():
    rid = request.json
    r = RoleManageService.delRole(rid)
    if type(r) == bool:
        if r:
            return R.success(None,"角色删除成功")
        else:
            return R.bad_request('角色删除失败')
    else:
        return R.bad_request(r)


# 查询角色拥有的菜单
@route("/role/get/menus")
def roleGetMenus():
    rid = request.args.get('rid')
    r = RoleManageService.getRoleMenus(rid)
    return R.success(r)

# 修改角色菜单
@route("/role/update/menus" ,methods = ['POST'])
def roleUpdateMenus():
    data:dict = request.json
    r = RoleManageService.updateRoleMenus(data.get('rid'),data.get('mids'),data.get('tags'))
    if(r):
        return R.success(r,'操作成功')
    return R.bad_request('更改失败')

# 查询角色拥有的操作权限
@route("/role/get/handles")
def roleGetHandles():
    rid = request.args.get('rid')
    r = RoleManageService.getHandleById(rid)
    return R.success(r)


@route("/role/handles_update", methods=['POST'])
def updateRoleHandle():
    data = request.json
    r = RoleManageService.updateRoleHandle(data['rid'],data['data'])
    if(r):
        return R.success(r,'保存成功')
    return R.bad_request('保存失败')